<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Report extends Admin_Controller {	
	/*------------------------------------ Admin Control Panel ------------------------------------*/
	public function admincp_index(){
		admin_check_permission($this->module_id,'r',true);
		
		$data = array();
		$data['from'] = date('d-m-Y',strtotime("-1 year"));
		$data['to'] = date('d-m-Y');
		$this->template->write_view('content','BACKEND/index',$data);
		$this->template->render();	
	}

	function admincp_report_submit(){
		if($this->input->post('action')){
			$from = $this->input->post('caledar_from');
			$from_array = split('-',$from);
			$from = mktime(0,0,0,$from_array[1],$from_array[0],$from_array[2]);
			$from = date(DATETIME_FORMAT_DB,$from);
			$data['from'] = $from;
			$to = $this->input->post('caledar_to');
			$to_array = split('-',$to);
			$to = mktime(23,59,59,$to_array[1],$to_array[0],$to_array[2]);
			$to = date(DATETIME_FORMAT_DB,$to);

			$html_top = '
				<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
				<html xmlns="http://www.w3.org/1999/xhtml">
					<head>
						<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
					</head>
					<body>
						<table width="1191" cellpadding="0" cellspacing="0" border="1" style="font-size:12px;">';
			$html_bot = '
						</table>
					</body>
				</html>';

			$filename = "Report";
			$action = $this->input->post('action');
			
			switch($action){
				case 'Report auction':
					$filename .= '_auctions';
					$query = "
						SELECT tb_auction.*,tb_user.email, tb_auction_bid.auction_id, COUNT(*) AS bid_count
						FROM tb_auction INNER JOIN tb_auction_bid ON tb_auction.id = tb_auction_bid.auction_id
										INNER JOIN tb_user ON tb_auction.winner_id = tb_user.id
						WHERE tb_auction.status = 1 AND is_ended = 1 AND end_time >= '{$from}' AND end_time <= '{$to}'
						GROUP BY tb_auction_bid.auction_id
						ORDER BY tb_auction.id ASC";

					$query = $this->db->query($query);
					$auction_list = $query->result_array();
					if(!empty($auction_list)){
						
						$head_html = '
							<tr height="20" bgcolor="#0066FF">
								<td height="30" colspan="8" align="center">
									<b  style="color: #FFFFFF;font-size:20px;">
										DANH SÁCH PHIÊN ĐẤU GIÁ TỪ '.$from.' ĐẾN '.$to.
									'</b>
								</td>
							</tr>
							<tr height="20">
								<td colspan="8" height="20"></td>
							  </tr>
							<tr height="20" bgcolor="#0066FF">
								<td colspan="8" height="20">
									<strong  style="color: #FFFFFF">Số phiên đấu giá:  '.count($auction_list).' phiên</strong>
								</td>
							</tr>
							<tr align="center">
								<td width="42" valign="top"><strong>STT</strong></td>
								<td width="192" valign="top"><strong>Tên sản phẩm</strong></td>
								<td width="192" valign="top"><strong>Giờ kết thúc</strong></td>
								<td width="192" valign="top"><strong>Giá thắng ('.MONETARY_UNIT.')</strong></td>
								<td width="192" valign="top"><strong>Người thắng</strong></td>
								<td width="192" valign="top"><strong>Số lượt đấu giá</strong></td>
							</tr>';

						$html = $html_top;
						$html .= $head_html;
						$n = 1;
						foreach ($auction_list as $item) {
							$html .= '
								<tr height="20">
								<td height="20" align="right">'.$n.'</td>
								<td>'.$item['title'].'</td>
								<td>'.$item['end_time'].'</td>
								<td>'.$item['winner_price_vn'].'</td>
								<td>'.$item['email'].'</td>
								<td>'.$item['bid_count'].'</td>
							</tr>';
							$n++;
						}

						$html .= $html_bot;
						
						// xóa session & send file
						// header("Content-type: application/x-www-form-urlencoded\r\n" );
						// header('Content-Disposition: attachment; filename="'.$filename.'.xls"');
						echo $html;
					}
					break;
			}
		}
	}
	/*------------------------------------ End Admin Control Panel --------------------------------*/
	
	
	/*------------------------------------ FRONTEND ------------------------------------*/
	
	/*------------------------------------ End FRONTEND --------------------------------*/
}